<?php

if (!defined('BASEPATH'))
    exit('Tidak Diperkenankan mengakses langsung');
/* Class  Control : kehadiran  * di Buat oleh Diar PHP Generator  *  By Diar */

class ctrkehadiran extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    function index($xAwal = 0, $xSearch = '') {
//  $this->load->view('test.php');
        $idpegawai = $this->session->userdata('idpegawai');
        if (empty($idpegawai)) {
            redirect(site_url(), '');
        }
        if ($xAwal <= -1) {
            $xAwal = 0;
        } $this->session->set_userdata('awal', $xAwal);
        $this->createformkehadiran('0', $xAwal);
    }

    function createformkehadiran($xidx, $xAwal = 0, $xSearch = '') {
        $this->load->helper('form');
        $this->load->helper('html');
        $this->load->model('modelgetmenu');
        $xAddJs = '<script language="javascript" type="text/javascript" src="' . base_url() . 'resource/ajax/baseurl.js"></script>' .
                '<script language="javascript" type="text/javascript" src="' . base_url() . 'resource/ajax/ajaxkehadiran.js"></script>';
        echo $this->modelgetmenu->SetViewAdmin($this->setDetailFormkehadiran($xidx), '', '', $xAddJs, '');
    }

    function setDetailFormkehadiran($xidx) {
        $this->load->helper('form');
        $xBufResult = '';
        $xBufResult = '<div id="stylized" class="myform"><h3>kehadiran</h3><div class="garis"></div>' . form_open_multipart('ctrkehadiran/inserttable', array('id' => 'form', 'name' => 'form'));
        $this->load->helper('common');
        $xBufResult .= '<input type="hidden" name="edidx" id="edidx" value="0" />';
//        $xBufResult .= setForm('edidcustomer', 'idcustomer', form_input(getArrayObj('edidcustomer', '', '100'))) . '<div class="spacer"></div>';
//        $xBufResult .= setForm('edtanggal', 'tanggal', form_input(getArrayObj('edtanggal', '', '100'))) . '<div class="spacer"></div>';
//        $xBufResult .= setForm('edjam', 'jam', form_input(getArrayObj('edjam', '', '100'))) . '<div class="spacer"></div>';
//        $xBufResult .= setForm('edketerangan', 'keterangan', form_input(getArrayObj('edketerangan', '', '100'))) . '<div class="spacer"></div>';
//        $xBufResult .= setForm('edtglupdate', 'tglupdate', form_input(getArrayObj('edtglupdate', '', '100'))) . '<div class="spacer"></div>';
//        $xBufResult .= setForm('edtglinsert', 'tglinsert', form_input(getArrayObj('edtglinsert', '', '100'))) . '<div class="spacer"></div>';
//        $xBufResult .= setForm('edidpegawai', 'idpegawai', form_input(getArrayObj('edidpegawai', '', '100'))) . '<div class="spacer"></div>';
        $xBufResult .= '<div class="garis"></div><div class="spacer"></div><div id="tabledatacustomer">' . $this->getlistcustomer(0, '') . '</div><div class="spacer"></div>';
        return $xBufResult;
    }

    function getlistkehadiran($xAwal, $xSearch) {
        $xLimit = 10;
        $this->load->helper('form');
        $this->load->helper('common');
        $xbufResult = tbaddrow(tbaddcell('idx', '', 'width=10%') .
                tbaddcell('idcustomer', '', 'width=10%') .
                tbaddcell('tanggal', '', 'width=10%') .
                tbaddcell('jam', '', 'width=10%') .
                tbaddcell('keterangan', '', 'width=10%') .
                tbaddcell('tglupdate', '', 'width=10%') .
                tbaddcell('tglinsert', '', 'width=10%') .
                tbaddcell('idpegawai', '', 'width=10%') .
                tbaddcell('Edit/Hapus', 'padding:5px;', 'width:10%;text-align:center;'), '', TRUE);
        $this->load->model('modelkehadiran');
        $xQuery = $this->modelkehadiran->getListkehadiran($xAwal, $xLimit, $xSearch);
        foreach ($xQuery->result() as $row) {
            $xButtonEdit = '<img src="' . base_url() . 'resource/imgbtn/edit.png" alt="Edit Data" onclick = "doeditkehadiran(\'' . $row->idx . '\');" style="border:none;width:20px"/>';
            $xButtonHapus = '<img src="' . base_url() . 'resource/imgbtn/delete_table.png" alt="Hapus Data" onclick = "dohapuskehadiran(\'' . $row->idx . '\',\'' . substr($row->idcustomer, 0, 20) . '\');" style="border:none;">';
            $xbufResult .= tbaddrow(tbaddcell($row->idx) .
                    tbaddcell($row->idcustomer) .
                    tbaddcell($row->tanggal) .
                    tbaddcell($row->jam) .
                    tbaddcell($row->keterangan) .
                    tbaddcell($row->tglupdate) .
                    tbaddcell($row->tglinsert) .
                    tbaddcell($row->idpegawai) .
                    tbaddcell($xButtonEdit . '&nbsp/&nbsp' . $xButtonHapus));
        }
        $xInput = form_input(getArrayObj('edSearch', '', '200'));
        $xButtonSearch = '<img src="' . base_url() . 'resource/imgbtn/b_view.png" alt="Search Data" onclick = "dosearchkehadiran(0);" style="border:none;width:30px;height:30px;" />';
        $xButtonPrev = '<img src="' . base_url() . 'resource/imgbtn/b_prevpage.png" style="border:none;width:20px;" onclick = "dosearchkehadiran(' . ($xAwal - $xLimit) . ');"/>';
        $xButtonNext = '<img src="' . base_url() . 'resource/imgbtn/b_nextpage.png" style="border:none;width:20px;" onclick = "dosearchkehadiran(' . ($xAwal + $xLimit) . ');" />';
        $xbufResult .= tbaddrow(tbaddcell($xInput . $xButtonSearch, '', 'width=10% colspan=2') .
                tbaddcell($xButtonPrev . '&nbsp&nbsp' . $xButtonNext, '', 'width=40% colspan =10'), '', TRUE);
        $xbufResult = tablegrid($xbufResult);
        return '<div class="tabledata"  style="width:100%;left:-12px;">' . $xbufResult . '</div>';
    }

    function editreckehadiran() {
        $xIdEdit = $_POST['edidx'];
        $this->load->model('modelkehadiran');
        $row = $this->modelkehadiran->getDetailkehadiran($xIdEdit);
        $this->load->helper('json');
        $this->json_data['idx'] = $row->idx;
        $this->json_data['idcustomer'] = $row->idcustomer;
        $this->json_data['tanggal'] = $row->tanggal;
        $this->json_data['jam'] = $row->jam;
        $this->json_data['keterangan'] = $row->keterangan;
        $this->json_data['tglupdate'] = $row->tglupdate;
        $this->json_data['tglinsert'] = $row->tglinsert;
        $this->json_data['idpegawai'] = $row->idpegawai;
        echo json_encode($this->json_data);
    }

    function deletetablekehadiran() {
        $edidx = $_POST['edidx'];
        $this->load->model('modelkehadiran');
        $this->modelkehadiran->setDeletekehadiran($edidx);
        $this->load->helper('json');
        echo json_encode(null);
    }

    function searchkehadiran() {
        $xAwal = $_POST['xAwal'];
        $xSearch = $_POST['xSearch'];
        $this->load->helper('json');
        if (($xAwal + 0) == -99) {
            $xAwal = $this->session->userdata('awal', $xAwal);
        }
        if ($xAwal + 0 <= -1) {
            $xAwal = 0;
            $this->session->set_userdata('awal', $xAwal);
        } else {
            $this->session->set_userdata('awal', $xAwal);
        }
        $this->json_data['tabledatakehadiran'] = $this->getlistkehadiran($xAwal, $xSearch);
        echo json_encode($this->json_data);
    }

    function simpankehadiran() {
        $this->load->helper('json');
        $this->load->helper('common');
        if (!empty($_POST['edidx'])) {
            $xidx = $_POST['edidx'];
        } else {
            $xidx = '0';
        }
        $xidcustomer = $_POST['edidcustomer'];
        $xtanggal = $_POST['edtanggal'];
        $xjam = $_POST['edjam'];
        $xketerangan = $_POST['edketerangan'];
        $xtglupdate = $_POST['edtglupdate'];
        $xtglinsert = $_POST['edtglinsert'];
        $xidpegawai = $_POST['edidpegawai'];
        $this->load->model('modelkehadiran');
        $idpegawai = $this->session->userdata('idpegawai');
        if (!empty($idpegawai)) {
//            if ($xidx != '0') {
//                $xStr = $this->modelkehadiran->setUpdatekehadiran($xidx, $xidcustomer, $xtanggal, $xjam, $xketerangan, $xtglupdate, $xtglinsert, $xidpegawai);
//            } else {
                 $this->modelkehadiran->setInsertkehadiran($xidx, $xidcustomer, $xtanggal, $xjam, $xketerangan, $xtglupdate, $xtglinsert, $idpegawai);
            //}
        }
        $row = $this->modelkehadiran->getLastIndexkehadiranByIdCust($xidcustomer);
        $this->load->helper('json');
        $this->json_data['idx'] = $row->idx;
        $this->json_data['idcustomer'] = $row->idcustomer;
        $this->json_data['tanggal'] =  datetomysql($row->tanggal);
        $this->json_data['jam'] = $row->jam;        
        echo json_encode($this->json_data);
//        echo json_encode(null);
    }

    function getlistcustomer($xAwal, $xSearch) {
        $xLimit = 20;
        $this->load->helper('form');
        $this->load->helper('common');
        $xbufResult = tbaddrow(tbaddcell('No', '', 'width=5%') .
                tbaddcell('Name', '', 'width=30%') .
                tbaddcell('Datae', '', 'width=10%') .
                tbaddcell('Jam', '', 'width=10%') .                
                tbaddcell('Discription', '', 'width=40%') .
                tbaddcell('Save', 'padding:5px;', 'width:10%;text-align:center;'), '', TRUE);
        $this->load->model('modelcustomer');
        $this->load->model('modelkehadiran');
        $xQuery = $this->modelcustomer->getListcustomer($xAwal, $xLimit, $xSearch);
        $xno = $xAwal+1;
        foreach ($xQuery->result() as $row) {
            $rowkehadiran = $this->modelkehadiran->getLastIndexkehadiranByIdCust($row->idx);
            $xButtonEdit = '<img src="' . base_url() . 'resource/imgbtn/btnsave.jpg"  alt="Edit Data" onclick = "dosimpankehadiran(\'' . $row->idx . '\');" style="border:none;width:40px;height:40px;"/>';
//            $xButtonHapus = '<img src="' . base_url() . 'resource/imgbtn/delete_table.png" alt="Hapus Data" onclick = "dohapuscustomer(\'' . $row->idx . '\',\'' . substr($row->title, 0, 20) . '\');" style="border:none;">';
            $xbufResult .= tbaddrow(tbaddcell($xno++) .
                    tbaddcell($row->title . ' ' . $row->firstname . ' ' . $row->midlename . ' ' . $row->lastname) .
                    tbaddcell('<div id="tanggal'.$row->idx.'">'.datetomysql(@$rowkehadiran->tanggal).'</div>') .
                    tbaddcell('<div id="jam'.$row->idx.'">'.@$rowkehadiran->jam.'</div>') .                    
                    tbaddcell(form_textarea(getArrayObj('edketerangan_' . $row->idx, @$rowkehadiran->keterangan, '300'))) .
                    tbaddcell($xButtonEdit));
        }
        $xInput = form_input(getArrayObj('edSearch', '', '200'));
        $xButtonSearch = '<img src="' . base_url() . 'resource/imgbtn/b_view.png" alt="Search Data" onclick = "dosearchcustomer(0);" style="border:none;width:30px;height:30px;" />';
        $xButtonPrev = '<img src="' . base_url() . 'resource/imgbtn/b_prevpage.png" style="border:none;width:20px;" onclick = "dosearchcustomer(' . ($xAwal - $xLimit) . ');"/>';
        $xButtonNext = '<img src="' . base_url() . 'resource/imgbtn/b_nextpage.png" style="border:none;width:20px;" onclick = "dosearchcustomer(' . ($xAwal + $xLimit) . ');" />';
        $xbufResult .= tbaddrow(tbaddcell($xInput . $xButtonSearch, '', 'width=10% colspan=2') .
                tbaddcell($xButtonPrev . '&nbsp&nbsp' . $xButtonNext, '', 'width=40% colspan =10'), '', TRUE);
        $xbufResult = tablegrid($xbufResult);
        return '<div class="tabledata"  style="width:100%;left:-12px;">' . $xbufResult . '</div>';
    }
    
  function searchcustomer() {
        $xAwal = $_POST['xAwal'];
        $xSearch = $_POST['xSearch'];
        $this->load->helper('json');
        if (($xAwal + 0) == -99) {
            $xAwal = $this->session->userdata('awal', $xAwal);
        }
        if ($xAwal + 0 <= -1) {
            $xAwal = 0;
            $this->session->set_userdata('awal', $xAwal);
        } else {
            $this->session->set_userdata('awal', $xAwal);
        }
        $this->json_data['tabledatacustomer'] = $this->getlistcustomer($xAwal, $xSearch);
        echo json_encode($this->json_data);
    }  
}

?>